import pygame, sys, os
from pygame.locals import *

# Initialise Pygame
pygame.init()

# Color alias tables
black    = [   0,   0,   0]
white    = [ 255, 255, 255]
blue     = [   0,   0, 255]
green    = [   0, 255,   0]
red      = [ 255,   0,   0]

# Function aliases
font = pygame.font.Font(None, 25)
clock = pygame.time.Clock()

# Load 
spyChar = pygame.image.load('../../graphics/spy01.png')
cursorObj = pygame.image.load('../../graphics/reticule.png')

# Set variable
bullets = 8
bullet_exist = 0
reserve = 80
mouse_x = mouse_y = 0
player_x = player_y = 10
speed_x = speed_y = 0

# Set display window.
screen = pygame.display.set_mode((1024, 768))
pygame.display.set_caption('Agitprop 0.01a')

# Function to calculate vector of bullet
def calculate_vector(player_x, player_y, mouse_x, mouse_y):
    origin_x = player_x + 82
    origin_y = player_y + 13
    rise = mouse_y - origin_y
    run = mouse_x - origin_x
    gradient = rise/run

# Function to draw the background
def draw_background(screen):
    screen.fill(white)

# Function to draw player
def draw_player(screen, x, y):
    screen.blit(spyChar, (x, y))

# Function to draw ammo
def draw_ammo(screen, clip, reserve):
    ammoTxt = font.render(str(clip) + " - " + str(reserve), True, black)
    screen.blit(ammoTxt, [120, 5])

# Function to draw bullet
def draw_bullet(screen, origin_x, origin_y, gradient):
    pygame.draw.line(screen, black, (origin_x, origin_y), (mouse_x, mouse_y), 4)

# Function to draw reticule
def draw_reticule(screen, x, y):
    screen.blit(cursorObj, (x, y))

# Begin game loop
done = False
while done == False:

    # Event loop
    for event in pygame.event.get():

        # Check for quit
        if event.type == pygame.QUIT:
            done = True

        # Checking for key presses
        if event.type == pygame.KEYDOWN: 

            # Checks movement
            if event.key == pygame.K_a: 
                speed_x = -3
            if event.key == pygame.K_d: 
                speed_x = 3
            if event.key == pygame.K_w: 
                speed_y = -3
            if event.key == pygame.K_s: 
                speed_y = 3

        # User let up on a key
        if event.type == pygame.KEYUP:

            # If the arrows keys are let go of, the speed drops.
            if event.key == pygame.K_a:
                speed_x = 0
            if event.key == pygame.K_d:
                speed_x = 0
            if event.key == pygame.K_w:
                speed_y = 0
            if event.key == pygame.K_s:
                speed_y = 0

        if event.type == pygame.MOUSEMOTION:
            mouse_x, mouse_y = event.pos        

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 0:
            bullet_exist = 1
            calculate_vector(player_x, player_y, mouse_x, mouse_y)
            draw_bullet(screen, x, y, gradient)
    
    # Move the spy according to the speed vector
    player_x += speed_x
    player_y += speed_y

    # Draw objects, update screen, set FPS
    draw_background(screen)
    draw_ammo(screen, bullets, reserve)
    draw_player(screen, player_x, player_y)
    draw_reticule(screen, mouse_x, mouse_y)
    if bullet_exist == 1:
        draw_bullet(screen, origin_x, origin_y, gradient)
    pygame.display.flip()
    clock.tick(40)
    
pygame.quit()
